package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import game.Player;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


/**
 * ScrollPane comprenant la liste des joueurs
 * 
 * @author David Fassler (modif par Jonathan gander)
 * @version 1.0
 */
public class JspPlayersInfos extends JScrollPane {
	
	private static final long serialVersionUID = 3784761518959806170L;
	
	/**
	 * Dimenssions de la fenetre
	 */
	public static int WIDTH = JPanelPlayerInfo.WIDTH+25;		
	/**
	 * Dimenssions de la fenetre
	 */
	public static int HEIGHT = 300;
	
	private Dimension dim = new Dimension(WIDTH, HEIGHT);
	private JPanel container;
	
	private Player[] players;

	
	/**
	 * Constructeur
	 * Permet de realiser un scrollPane contant les joueurs
	 * 
	 * @param showScore pour afficher le score du joueur
	 */
	public JspPlayersInfos(boolean showScore){
		setPreferredSize(dim);
		container = new JPanel();
		container.setBackground(Color.white);
		container.setLayout(new FlowLayout(FlowLayout.LEFT,0,1));
	}
	
	/**
	 * Constructeur
	 * Permet de realiser un scrollPane contant les joueurs
	 * 
	 * @param players liste des joueurs a lister
	 * @param showScore pour afficher le score du joueur
	 */
	public JspPlayersInfos(Player[] players, boolean showScore){
		this(showScore);
		this.players = players;
		container.setPreferredSize(new Dimension(JPanelPlayerInfo.WIDTH, players.length*JPanelPlayerInfo.HEIGHT+JPanelPlayerInfo.HEIGHT));
		container.add(new JPanelPlayerInfo(showScore));
		// On ajoute un panel pour chaque joueurs dans le container
		for(Player player:players){
			JPanelPlayerInfo playerInfo = new JPanelPlayerInfo(player,showScore);
			playerInfo.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
			container.add(playerInfo);
		}
		
		this.setViewportView(container);
		
	}
	
	/**
	 * Permet rafraichir la liste les joueurs
	 * 
	 * @param players liste des joueurs a lister
	 * @param showScore pour afficher le score du joueur
	 * @param nextPlayer Indique le joueur suivant
	 */
	public void refreshGames(Player[] players, boolean showScore, Player nextPlayer){
		container.removeAll();
		container.setPreferredSize(new Dimension(JPanelPlayerInfo.WIDTH, players.length*JPanelPlayerInfo.HEIGHT+JPanelPlayerInfo.HEIGHT));
		container.add(new JPanelPlayerInfo(showScore));
		for( Player player:players)
			container.add(new JPanelPlayerInfo(player, showScore));
		setViewportView(container);
		if (nextPlayer != null)
			this.showPlayerInGame(nextPlayer);
		this.repaint();
	}
	
	/**
	 * Permet de marquer un certain joueur dans la liste
	 *
	 * @param p Player a marquer
	 */
	public void showPlayerInGame(Player p){
		for (int i = 0; i < this.players.length; i++) {
			container.getComponent(i + 1).setBackground(this.players[i].getColor());
			if (this.players[i].getPseudo().equals(p.getPseudo())){
				((JPanel)container.getComponent(i + 1)).setBorder(BorderFactory.createLineBorder(Color.RED,2));
			}
		}
		this.repaint();
	}

}
